package com.dotflat.training.service.impl;

import com.dotflat.rest.service.impl.AreableObjectBaseServiceImpl;
import com.dotflat.training.domain.OTCourseBookRecord;
import com.dotflat.training.repository.OTCourseBookRecordRepository;
import com.dotflat.training.repository.OTCourseCheckRecordRepository;
import com.dotflat.training.service.OTCourseBookRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class OTCourseBookRecordServiceImpl extends AreableObjectBaseServiceImpl<OTCourseBookRecord, OTCourseBookRecordRepository> implements OTCourseBookRecordService {

    @Autowired
    @Override
    public void setBaseRepository(OTCourseBookRecordRepository repository){
        super.setBaseRepository(repository);
    }

    @Autowired
    private OTCourseCheckRecordRepository checkRecordRepository;


//    @Override
//    public Page<OTCourseBookRecord> page(String areaId, String staffId, AreableObjectBasePO param, PageRequest pageRequest) {
//
//        param.setAreaId(areaId);
//        param.setDeleted(false);
//
//        Specification<OTCourseBookRecord> specification = new Specification<OTCourseBookRecord>() {
//            @Override
//            public Predicate toPredicate(Root<OTCourseBookRecord> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
//                List<Predicate> predicates = new ArrayList<>();
//
//                generatePagePredicates(root, query, criteriaBuilder, param, predicates);
//
//                //创建左连接
//
//                Join<OTCourseBookRecord, OTCourseCheckRecord> b = root.join("checkRecords", JoinType.LEFT); //left outer join
//                b.on(
//                        criteriaBuilder.and(
//                                criteriaBuilder.equal(b.get("offlineCourseId"), root.get("offlineCourseId")),
//                                criteriaBuilder.equal(b.get("staffId"), root.get("staffId")),
//                                criteriaBuilder.equal(b.get("deleted"), root.get("deleted"))
//                        )
//                );
//
//                Expression<Boolean> caseHasCreated = criteriaBuilder.<Boolean>selectCase()
//                        .when(criteriaBuilder.isNotNull(b.get("created")), true)
//                        .otherwise(false);
//
//                query.multiselect(caseHasCreated.as(Boolean.class));
//
////                query.select(criteriaBuilder.isNotNull(b.get("created")).as("checked"));
//
//                query.where(predicates.toArray(new Predicate[predicates.size()]));
//
//                return query.getRestriction();
//            }
//        };
//
//
//        Page<OTCourseBookRecord> page = baseRepository.findAll(specification, pageRequest);
//
//        parsePageResult(areaId, staffId, param, pageRequest, page);
//
//        return page;
//    }
//
//    @Override
//    protected void parsePageResult(String areaId, String staffId, AreableObjectBasePO param, PageRequest pageRequest, Page<OTCourseBookRecord> page){
//        Page<String> idsPage = page.map(o->o.getOfflineCourseId());
//        List<String> ids = idsPage.getContent();
//        List<OTCourseCheckRecord> records = checkRecordRepository.findAllByAreaIdAndStaffIdAndOfflineCourseIdInAndDeletedIsFalse(areaId, staffId, ids);
//
//        Map<String,OTCourseCheckRecord> cache = records.stream().collect(Collectors.toMap(OTCourseCheckRecord::getOfflineCourseId, o->o));
//
//        page.forEach(o->{
//            o.setChecked(cache.get(o.getOfflineCourseId()) != null);
//        });
//    }
}